package com.krahets88.hashtable;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.HashSet;

public class Question409 {
    @Test
    public void test1(){
        Solution409 solve = new Solution409();
        System.out.println(solve.longestPalindrome("bb"));
    }
}

class Solution409 {
    public int longestPalindrome(String s) {
        /*
        遍历字符串，获取每个字符的出现次数 2*n+1
         */
        HashMap<Character, Integer> dict = new HashMap<>();
        for(int i=0;i<s.length();i++){
            if(dict.containsKey(s.charAt(i))){
                dict.put(s.charAt(i),dict.get(s.charAt(i))+1);
            }else {
                dict.put(s.charAt(i),1);
            }
        }
        int n=0;
        for(int val:dict.values()){
            n=n+val/2;
        }
        if(2*n==s.length()){
            return 2*n;
        }else {
            return 2*n+1;
        }
    }
}

class Solution409Two {
    public int longestPalindrome(String s) {
        /*
        遍历字符串，直接用set集合算
         */
        HashSet<Character> set = new HashSet<Character>();
        int res=0;
        for(int i=0;i<s.length();i++){
            if(set.contains(s.charAt(i))){
                res+=2;
                set.remove(s.charAt(i));
            }else {
                set.add(s.charAt(i));
            }
        }
        if(!set.isEmpty()){
            return res+1;
        }else {
            return res;
        }
    }
}